import Vue from 'vue'
import Router from 'vue-router'
import router from './config/router'
import global from './constant/Global'

let GLOBAL = global

Vue.use(Router)

// 解决ElementUI导航栏中的vue-router在3.0版本以上重复点菜单报错问题
const originalPush = Router.prototype.push
Router.prototype.push = function push (location) {
  return originalPush.call(this, location).catch(err => err)
}

// 路由配置
const route = new Router({
  routes: router
})

// 路由前检查token
route.beforeEach((to, from, next) => {
  if (to.path === '/login') {
    next()
  } else {
    let token = localStorage.getItem(GLOBAL.localStoreKey.token)
    if (token === null || token === '') {
      next('/login')
    } else {
      next()
    }
  }

  next()
})

export default route
